<?php
	require_once( "functions/inc_systemFunctions.php" );
	page_header( "Wayne Schlegel Inc. | Product reviews", true, "myflight" );
?>

<table class="iTable">
	<tr class="moreInformation">
		<td><a href="product-reviews-add.php?pid=<?=$_GET['pid']?>">add review</a></td>
	</tr>
	<tr class="moreInformation lastItem">
		<td><a href="product-details.php?pid=<?=$_GET['pid']?>">product details</a></td>
	</tr>
</table>

<p><small>Note: Reviews marked with an asterisk (*) are external reviews.</small></p>

<table class="iTable">
<?php
	if( empty( $_GET['pid'] ) ){
		echo "<tr class=\"lastItem\"><td>Sorry, no reviews available.</td></tr>\n";
	} else {
		$cnt_reviewsShown = 0;
		$build_css        = "";
		$build_js         = "";
		# show internal reviews
		$sql = "SELECT * FROM reviews WHERE product_id = " . $_GET['pid'] . " AND ( (userData_id = " . $_SESSION['user_id'] . ") OR (isApproved = 1) )";
		$res = mysql_query( $sql );
		if( $res ){
			if( mysql_num_rows( $res ) == 0 ){
				echo "<tr class=\"lastItem\"><td>Sorry, no reviews available.</td></tr>\n";
			}
			while( $row = mysql_fetch_array( $res ) ){
				$cnt_reviewsShown++;
				echo "<tr class=\"moreInformation\">";
				echo "<td>";
				if( $row['isApproved'] == 0 ){
					echo "<em>";
				}
				echo "<a href=\"#\" id=\"showReview" . $row['id'] . "\">" . $row['title'] . "</a>";
				if( $row['isApproved'] == 0 ){
					echo "</em>";
				}
				echo "<p id=\"_review" . $row['id'] . "\">" . $row['text'] . "</p></td>";
				$build_css .= "#_review" . $row['id'] . "{display: none;}";
				$build_js  .= '$(function(){$("#showReview' . $row['id'] . '").click(function(event){event.preventDefault();$("#_review' . $row['id'] . '").slideToggle();});$("#_review' . $row['id'] . ' a").click(function(event) {event.preventDefault();$("#_review' . $row['id'] . '").slideUp();});});';
				echo "</tr>";
			}
		} else {
			echo "<tr class=\"lastItem\"><td>Sorry, no reviews available.</td></tr>\n";
		}
		if( $cnt_reviewsShown < numberOfReviewsPerPage ){
			# get external reviews
			$currentProduct = getProduct( $_GET['pid'] );
			if( !empty( $currentProduct['googleReviewSearchString'] ) ) {
				$review = getReviews( urlencode( $currentProduct['googleReviewSearchString'] ) );
#				echo "<pre>";
#				var_dump( $review->items );
#				echo "</pre>";
				$numberOfReviews = count( $review->items );
				if( !empty( $review ) && $numberOfReviews > 0 ){
					foreach( $review->items as $item ){
						if( $cnt_reviewsShown < numberOfReviewsPerPage ){
							$cnt_reviewsShown++;
							echo "<tr class=\"moreInformation";
							if( $cnt_reviewsShown == numberOfReviewsPerPage ){
								echo " lastItem";
							}
							echo "\"><td><a href=\"" . $item['link'] . "\" target=\"_ws_reviews\">" . htmlentities( substr( $item['title'], 0, 20 ) ) . "&hellip; *</a></td></tr>\n";

						}
					}
				}
			}
		}
	}
?>
</table>

<style type="text/css">
<?=$build_css?>
</style>

<script type="text/javascript">
<?=$build_js?>
</script>


<?php
	page_footer();
?>